var express = require('express');
var userRouter = express.Router();
const user = require('../dao/user_dao')
const fs = require('fs')
let multer = require('multer')


// 登录请求api
userRouter.post('/login', (req, res) => {
  user.Login(req, res)
})

// 用户信息获取api
userRouter.get('/getUserDataByToken', (req, res) => {
  user.getUserDataByToken(req, res)
})

// 获取不同类型用户信息
userRouter.get('/getUsersByTypePage', (req, res) => {
  user.getUsersByTypePage(req, res)
})

// 删除用户
userRouter.get('/delUserdata', (req, res) => {
  user.delUserdata(req, res)
})


// 修改用户
userRouter.post('/upUserdata', (req, res) => {
  user.upUserdata(req, res)
})


// 添加用户
userRouter.post('/addUserdata', (req, res) => {
  user.addUserData(req, res)
})


/* ----------------修改个人信息--------------------- */
//修改密码
userRouter.post('/updatePassword', (req, res) => {
  user.updatePassword(req, res)
})

//上传头像
let multerArr = multer({
  dest: "public/upload",
}).array("file", 1)
userRouter.post("/uploadAvatar", multerArr, (req, res) => {
  let file = req.files[0];
  let fileInfo = {};

  // 文件重命名
  let path = "public/upload/" + Date.now().toString() + "_" + file.originalname;
  fs.renameSync("./public/upload/" + file.filename, path);

  //获取文件基本信息
  fileInfo.type = file.mimetype;
  fileInfo.name = file.originalname;
  fileInfo.size = file.size;
  fileInfo.path = path.substr(7);

  res.json({
    path: fileInfo.path
  })
});

// 修改头像
userRouter.post("/updateAvatar", multerArr, (req, res) => {
  user.updateAvatar(req, res)
});







module.exports = userRouter;